package com.qingluan.dd.InetTool;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class Internet_info_get {
	private String host;
	private int port = 50000;
	public Internet_info_get(){
		
	}
	public Internet_info_get(String url){
		this.host = url;
	}
	public Internet_info_get(String url,int port ){
		this.host = url;
		this.port = port;
	}
	public byte[] run() throws IOException {
		Socket client = new Socket();
		SocketAddress SA = new InetSocketAddress(this.host,this.port);
		client.connect(SA, 5000); //设置超时
		DataInputStream DIA = null;
		byte[] gotBuffer = null;
		try{
			while (true){
				DIA = new DataInputStream(client.getInputStream());
				if (DIA.available()>0){
					byte[] buffer = new byte[DIA.available()];
					DIA.read(buffer);
					gotBuffer = buffer;
				}
				if(gotBuffer != null){
					break;
				}
				
			}
		
		}catch (SocketTimeoutException e){
			String RequestFail= "Request timeout!";
			byte[] RFbyte = RequestFail.getBytes();
			return RFbyte;
		}
		if (gotBuffer != null){
			return gotBuffer;
		}
		return null;
				
	}
}
